package xyz.csmcool.imchat.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;

import java.util.HashMap;
import java.util.Map;

/**
 * @author 什锦
 * @Package xyz.csmcool.imchat.config
 * @since 2022/8/14 16:59
 */

@SpringBootConfiguration
public class RabbitmqConfigFanout {

    /** 日志打印*/
    private final Logger log = LoggerFactory.getLogger(RabbitmqConfigFanout.class);

    /***************************  初始化消息队列 ***************************/
    /**
     * 初始化队列A -> xx功能
     * 第二个参数TRUE开启消息签收
     */
    @Bean(name = "roomchat")
    public Queue queueA() {
        Map<String,Object> arguments = new HashMap<>(2);
        // 绑定该队列到私信交换机
        arguments.put("x-dead-letter-exchange","deadExchange");
        Queue queue = new Queue("roomchat",true,false,false,arguments);
        log.info("队列：[{}]初始化成功................",queue.getName());
        return queue;
    }

    @Bean(name = "privatechat")
    public Queue queueB() {
        Queue queue = new Queue("privatechat",true);
        log.info("队列：[{}]初始化成功................",queue.getName());
        return queue;
    }

    @Bean("broadcast1")
    public FanoutExchange fanoutExchange() {
        FanoutExchange fanoutExchange = new FanoutExchange("broadcast1");
        log.info("交换机：[{}]初始化成功................",fanoutExchange.getName());
        return fanoutExchange;
    }

    @Bean("broadcast2")
    public FanoutExchange fanoutExchange2() {
        FanoutExchange fanoutExchange = new FanoutExchange("broadcast2");
        log.info("交换机：[{}]初始化成功................",fanoutExchange.getName());
        return fanoutExchange;
    }

    @Bean("deadExchange")
    public FanoutExchange deadExchange() {
        return new FanoutExchange("deadExchange");
    }

    @Bean("deadQueue")
    public Queue deadQueue(){
        return new Queue("deadQueue", true);
    }



    /*绑定队列B到交换机1*/
    @Bean
    public Binding bindingQueueA(@Qualifier("roomchat") Queue queue,@Qualifier("broadcast1") FanoutExchange fanoutExchange) {
        Binding binding = BindingBuilder.bind(queue).to(fanoutExchange);
        log.info("消息队列：[{}] 成功绑定到交换机：[{}] ................",queue.getName(),fanoutExchange.getName());
        return binding;
    }

    /*绑定队列B到交换机2*/
    @Bean
    public Binding bindingQueueB(@Qualifier("privatechat") Queue queue,@Qualifier("broadcast2") FanoutExchange fanoutExchange) {
        Binding binding = BindingBuilder.bind(queue).to(fanoutExchange);
        log.info("消息队列：[{}] 成功绑定到交换机：[{}] ................",queue.getName(),fanoutExchange.getName());
        return binding;
    }

    /**
     * 死信交换器与死信队列绑定
     */
    @Bean
    Binding deadRouteBinding(@Qualifier("deadQueue") Queue queue,@Qualifier("deadExchange") FanoutExchange fanoutExchange) {
        Binding binding = BindingBuilder.bind(queue).to(fanoutExchange);
        return binding;
    }

}
